package com.gree.ssm.web.admin;

import com.gree.ssm.bean.Blog;
import com.gree.ssm.bean.PageBean;
import com.gree.ssm.common.CommonConstant;
import com.gree.ssm.common.CommonResult;
import com.gree.ssm.service.BlogService;
import com.gree.ssm.utils.StringUtil;
import net.sf.json.JsonConfig;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author :xiaossong
 * @Description: 管理员博客Controller层
 * @Date Created in 下午11:26 2017/10/18
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {

    @Resource
    private BlogService blogService;

    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list(@RequestParam(value = "page", required = false) String page,
                             @RequestParam(value = "rows",required = false) String rows, Blog blog){
        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("title", StringUtil.formatLike(blog.getTitle()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<Blog> blogList = blogService.list(map);

        long total = blogService.getTotal(map);
        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("rows",blogList);
        map1.put("total",total);
        return map1;
    }



    /**
     * 添加或者修改博客信息
     * @param blog
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/save")
    public CommonResult save(Blog blog)throws Exception{
        int resultTotal=0; // 操作的记录条数
        if(blog.getId()==null){
            resultTotal=blogService.add(blog);
           // blogIndex.addIndex(blog); // 添加博客索引
        }else{
            resultTotal=blogService.update(blog);
           // blogIndex.updateIndex(blog); // 更新博客索引
        }
        //JSONObject result=new JSONObject();
        if(resultTotal>0){
            //result.put("success", true);
            return new CommonResult(CommonConstant.SUCCESS_CODE,"success");
        }else{
            return new CommonResult(CommonConstant.FAIL_CODE,"error");
        }

    }

    //查询指定id
    /*@RequestMapping("/find")
    public String findById(@RequestParam(value = "id",required = true) Integer id,Model model){
        Blog  blog= blogService.findById(id);
        model.addAttribute("blog",blog);
        return "/admin/modifyBlog";
    }*/
    /**
     * 通过ID查找实体
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/findById")
    public Blog findById(@RequestParam(value="id")String id)throws Exception{
        Blog blog=blogService.findById(Integer.parseInt(id));

        return blog;
    }

    /**
     * 删除博客信息
     * @param ids
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/delete")
    public CommonResult delete(@RequestParam(value="ids")String ids )throws Exception{
        String []idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            blogService.delete(Integer.parseInt(idsStr[i]));
            //blogIndex.deleteIndex(idsStr[i]); // 删除对应博客的索引
        }
        return new CommonResult(CommonConstant.SUCCESS_CODE, "success");
    }
}
